//
//  XMLClass.m
//  test
//
//  Created by Christian Speich on 11.01.08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "XMLClass.h"
#import "Pupil.h"
#import "Subject.h"
#import "Mark.h"
#import "Lesson.h"

@implementation XMLClass

- (void)parseXMLFile:(NSString *)pathToFile {
    NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];
    if (xml)
        [xml release];
	if (pupil)
		[pupil release];
	
    xml = [[NSClassFromString(@"NSXMLParser") alloc] initWithContentsOfURL:xmlURL];

    [xml setDelegate:self];
    [xml setShouldResolveExternalEntities:YES];
	if (![xml parse]) {
		NSLog(@"Parsen schlug fehl");
	}
	NSLog(@"Es wurden %i Faecher gefunden.", [[pupil subjects] count]);
}

- (void)parseXMLData:(NSData *)data {
    if (xml)
        [xml release];
	if (pupil)
		[pupil release];
	
    xml = [[NSClassFromString(@"NSXMLParser") alloc] initWithData:data];
	
    [xml setDelegate:self];
    [xml setShouldResolveExternalEntities:YES];
	if (![xml parse]) {
		NSLog(@"Parsen schlug fehl");
	}
	NSLog(@"Es wurden %i Faecher gefunden.", [[pupil subjects] count]);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if ([elementName isEqualToString:@"pupil"]) {
		pupil = [[Pupil alloc] init];
	}
	else if ([elementName isEqualToString:@"subject"]) {
		currentSubject = [[Subject alloc] init];
		NSString *type = [attributeDict objectForKey:@"type"];
		[currentSubject setType:type];
	}
	else if ([elementName isEqualToString:@"mark"]) {
		currentMark = [[Mark alloc] init];
		NSString *type = [attributeDict objectForKey:@"type"];
		[currentMark setType:type];
		NSString *halfYear = [attributeDict objectForKey:@"halfYear"];
		[currentMark setHalfYear:[halfYear intValue]-1];
		if ([type isEqualToString:@"classTest"]) {
			 NSString *valency = [attributeDict objectForKey:@"valency"];
			 [currentMark setValency:[valency floatValue]];
		}
	}
	else if ([elementName isEqualToString:@"lesson"]) {
		currentLesson = [[Lesson alloc] init];
		NSString *day = [attributeDict objectForKey:@"day"];
		[currentLesson setDay:day];
		NSString *room = [attributeDict objectForKey:@"room"];
		[currentLesson setRoom:room];
		NSString *lesson = [attributeDict objectForKey:@"lesson"];
		[currentLesson setLesson:[lesson intValue]];
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
	if (!currentStringValue) {
        currentStringValue = [[NSMutableString alloc] initWithCapacity:1];
    }

	[currentStringValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	[self removeLeadingSpaces:currentStringValue];

	if ([elementName isEqualToString:@"name"]) {
		if (currentSubject) {
			[currentSubject setName:currentStringValue];
		}
		else {
			[pupil setName:currentStringValue];
		}
	}
	else if ([elementName isEqualToString:@"class"]) {
		[pupil setClass:currentStringValue];
	}
	else if ([elementName isEqualToString:@"subject"]) {
		[pupil addSubject:currentSubject];
		[currentSubject release];
		currentSubject = nil;
	}
	else if ([elementName isEqualToString:@"teacher"]) {
		if (currentSubject) {
			[currentSubject setTeacher:currentStringValue];
		}
	}
	else if ([elementName isEqualToString:@"mark"]) {
		[currentMark setMark:[currentStringValue intValue]];
		[currentSubject addMark:currentMark];
		[currentMark release];
		currentMark = nil;
	}
	else if ([elementName isEqualToString:@"lesson"]) {
		[currentSubject addLesson:currentLesson];
		[currentLesson release];
		currentLesson = nil;
	}
	else if ([elementName isEqualToString:@"currentHalfYear"]) {
		[pupil setCurrentHalfYear:[currentStringValue intValue]-1];
	}
		
	[currentStringValue release];
	currentStringValue = nil;
}

- (void)removeLeadingSpaces:(NSMutableString*)string
{
	int i, count = [string length];
	for (i = 0; i < count; i++) {
		unichar c = [string characterAtIndex:i];
		if (c != ' ' && c != '\n' && c != '\t') {
			break;
		}
	}
	
	if (i > 0) {
		[string replaceCharactersInRange:NSMakeRange(0,i) withString:@""];
	}
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
	NSLog(@"FATAL Parser Error %i, Description: %@, Line: %i, Column: %i", [parseError code],
		  [parser parserError], [parser lineNumber],
		  [parser columnNumber]);
}
	
- (Pupil*)pupil
{
	return [pupil retain];
}

- (void) dealloc
{
	[xml release];
	if (currentStringValue)
		[currentStringValue release];
	if (currentMark)
		[currentMark release];
	if (pupil)
		[pupil release];
	if (currentLesson)
		[currentLesson release];
	if (currentSubject)
		[currentSubject release];
	
	[super dealloc];
}


@end
